package 初级算法;

import java.util.HashMap;
import java.util.HashSet;

public class 两数之和 {
    public static void main(String[] args) {
        int[] nums = new int[]{2,7,11,15};
        for (int i : twoSum(nums, 9)) {
            System.out.println(i);
        }
    }

    public static int[] twoSum(int[] nums, int target) {
        HashMap<Integer,Integer> map = new HashMap();
        for (int i = 0; i < nums.length; i++) {
            if(map.get(nums[i])!=null){
                return new int[]{map.get(nums[i]),i};
            }
            map.put(target-nums[i],i);
        }
        return new int[]{0,0};
    }
}
